﻿var meb = angular.module('lController', []);

//权限管理-管理员列表
meb.controller('authorityController', function ($rootScope, $scope, $http, $sce, Msg, paging) {
    //初始化模态窗
    $scope.md = Msg;
    //初始化服务
    $scope.pagingModel = paging;
    //请求配置
    var getsortConfig;
    //新增管理员模型
    $scope.addModel = {}
    //编辑管理员模型
    $scope.editorModel = {
        AdminId: '',
        UserName: '',
        RealName: '',
        AdminEmail: '',
        AdminTel: '',
        RoleID: '',
        OldPassword: '',
        Password: '',
        Enabled: 0
    }
    //获取数据时的配置
    $scope.getAdminConfig = {};
    init();
    //页面初始化
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Authority/GetJson',
            data: {
                adminSearchModel: $scope.getAdminConfig
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.getAdminConfig.PageIndex = index;
                init();
            });
        }).error(function (data, status, headers, config) {
            $scope.addGm = false;
            $scope.editor = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }
    //确认密码
    $scope.PasswordTwo = '';
    //多个模态窗口显示隐藏
    $scope.addGm = false;
    //多个模态窗口显示隐藏
    $scope.editor = false;
    //新镇管理员
    $scope.addMsg = function () {
        $scope.addGm = true;
        $scope.editor = false;
        $scope.PasswordTwo = '';
        $scope.md.okShow('新增管理员', null, function () {
            getsortConfig = {
                method: 'POST',
                url: '/Authority/Add',
                data: {
                    admin: $scope.addModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.addGm = false;
                init();
                $scope.md.show('提示', '新增成功');
            }).error(function (data, status, headers, config) {
                $scope.addGm = false;
                $scope.md.title = '提示';
                $scope.md.content = '新增失败';
                $scope.md.okBtn = false;
            });
        });
    }
    //编辑管理员账户
    $scope.editorMsg = function (AdminId, UserName, RealName, AdminEmail, AdminTel, RoleId) {
        $scope.addGm = false;
        $scope.editor = true;
        $scope.PasswordTwo = '';
        $scope.editorModel.AdminId = AdminId;
        $scope.editorModel.UserName = UserName;
        $scope.editorModel.RealName = RealName;
        $scope.editorModel.AdminEmail = AdminEmail;
        $scope.editorModel.AdminTel = AdminTel;
        $scope.editorModel.RoleID = RoleId;
        $scope.md.okShow('编辑信息', '', function () {
            getsortConfig = {
                method: 'POST',
                url: '/Authority/Update',
                data: {
                    admin: $scope.editorModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.editor = false;
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.editor = false;
                $scope.md.title = '提示';
                $scope.md.content = '编辑失败';
                $scope.md.okBtn = false;
            });
        });
    }
    //重置管理员密码
    $scope.resetPassword = function (AdminId, UserName, RealName, AdminEmail, AdminTel, RoleId) {
        $scope.addGm = false;
        $scope.editor = false;
        $scope.editorModel.AdminId = AdminId;
        $scope.editorModel.UserName = UserName;
        $scope.editorModel.RealName = RealName;
        $scope.editorModel.AdminEmail = AdminEmail;
        $scope.editorModel.AdminTel = AdminTel;
        $scope.editorModel.RoleID = RoleId;
        getsortConfig = {
            method: 'POST',
            url: '/Authority/ResetPassWord',
            data: {
                admin: $scope.editorModel
            }
        }
        $http(getsortConfig).success(function (data) {

            $scope.md.show('提示', '重置密码成功');
        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '重置密码失败');
        });
    }

    //搜索
    $scope.search = function () {
        init();
    }
    //禁用启用
    $scope.enable = function (AdminId, UserName, RealName, AdminEmail, AdminTel, RoleId, Enabled) {
        $scope.editorModel.AdminId = AdminId;
        $scope.editorModel.UserName = UserName;
        $scope.editorModel.RealName = RealName;
        $scope.editorModel.AdminEmail = AdminEmail;
        $scope.editorModel.AdminTel = AdminTel;
        $scope.editorModel.RoleID = RoleId;
        $scope.editorModel.Enabled = Enabled;
        console.log($scope.editorModel);
        if (Enabled) {
            getsortConfig = {
                method: 'POST',
                url: '/Authority/Enable',
                data: {
                    admin: $scope.editorModel
                }
            }
        } else {
            getsortConfig = {
                method: 'POST',
                url: '/Authority/Pause',
                data: {
                    admin: $scope.editorModel
                }
            }
        }
        $http(getsortConfig).success(function (data) {
            init();
        }).error(function (data, status, headers, config) {
            $scope.editor = false;
            $scope.md.title = '提示';
            $scope.md.content = '操作失败';
            $scope.md.okBtn = false;
        });

    }
});

//权限管理-日志管理
meb.controller('authorityJournalController', function ($scope, $http, paging,Msg) {
    //初始化模态窗
    $scope.md = Msg;
    $scope.pagingModel = paging;
    $scope.getAdminConfig = {};
    init();
    //页面初始化
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Authority/JournalDetail',
            data: {
                adminLogSearchModel: $scope.getAdminConfig
            }
        }
        $http(getsortConfig).success(function (data) {
            
            $scope.model = data;
            console.log(data);
            //初始化分页
            if (data.PageList.lengt > 0) {
                $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                    $scope.getAdminConfig.PageIndex = index;
                    init();
                });
            } else {
                $scope.pagingModel.showMe = false;
            }
           
            
        }).error(function (data, status, headers, config) {
            $scope.addGm = false;
            $scope.editor = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }
});

//权限管理-角色管理
meb.controller('authorityRolesController', function ($scope, $http, $log, Msg, paging) {
    var getsortConfig;
    $scope.pagingModel = paging;
    $scope.getAdminConfig = {};
    //初始化模态窗
    $scope.md = Msg;
    //添加角色的属性
    $scope.RolesModel = {
        Name: '',
        Description: '',
        Commodity: {
        },
        Member: {
        }
    }
    $scope.editorModel = {};
    init();
    //页面初始化
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'POST',
            url: '/Role/GetJson',
            data: {
                menuPanel: $scope.addModel
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.getAdminConfig.PageIndex = index;
                init();
            });
        }).error(function (data, status, headers, config) {
            $scope.addGm = false;
            $scope.editor = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }
    //添加角色
    $scope.addRoles = function () {
        $scope.addRolesPanel = true;
        $scope.md.okShow('添加角色', '', function () {
            getDetails();
            getsortConfig = {
                method: 'POST',
                url: '/Role/Add',
                data: {
                    role: $scope.RolesModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.addRolesPanel = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.md.show('提示', '数据获取失败');
            });
        });
    }
    function getDetails() {
        $scope.RolesModel.UserAuthorizeList = [];
        var m = 0, n = 0;
        for (m, n; m < $scope.RoleDetail.RoleDetail.length;) {
            $scope.RoleDetail.RoleDetail[m].Authorize[n].type ? $scope.RolesModel.UserAuthorizeList.push($scope.RoleDetail.RoleDetail[m].Authorize[n]) : null;
            n++;
            if (n >= $scope.RoleDetail.RoleDetail[m].Authorize.length) {
                n = 0;
                m++;
            }
        }
    }
    $scope.editorMsg = function (Id, Name, Description) {
        $scope.RolesModel.Id = Id;
        $scope.RolesModel.Name = Name;
        $scope.RolesModel.Description = Description;
        $scope.RolesModel.UserAuthorizeList = $scope.RolesModel.UserAuthorizeList
        $scope.addRolesPanel = true;
        GetRoleDetail(Id);
        $scope.md.okShow('编辑角色', '', function () {
            getDetails();
            getsortConfig = {
                method: 'POST',
                url: '/Role/Update',
                data: {
                    role: $scope.RolesModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.addRolesPanel = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.md.show('提示', '数据获取失败');
            });
        });
    }
    $scope.enable = function (Id, IsEnabled) {
        $scope.editorModel.Id = Id;
        if (IsEnabled) {
            getsortConfig = {
                method: 'POST',
                url: '/Role/Enable',
                data: {
                    role: $scope.editorModel
                }
            }
        } else {
            getsortConfig = {
                method: 'POST',
                url: '/Role/Pause',
                data: {
                    role: $scope.editorModel
                }
            }
        }
        console.log(getsortConfig);
        $http(getsortConfig).success(function (data) { 
            init();
            if (data.ResultValue != 0) {
                $scope.editor = false;
                $scope.md.show('提示', data.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.editor = false;
            $scope.md.title = '提示';
            $scope.md.content = '操作失败';
            $scope.md.okBtn = false;
        });
    }
    GetEmptyRoleDetail();
    function GetRoleDetail(id) {
        getsortConfig = {
            method: 'GET',
            url: '/role/GetRoleDetail?id=' + id,
        }
        $http(getsortConfig).success(function (data) {
            var RoleDetailIdList = [];
            var m = 0, n = 0;
            for (m, n; m < data.RoleDetail.length;) {
                data.RoleDetail[m].Authorize[n].Id ? RoleDetailIdList.push(data.RoleDetail[m].Authorize[n].Id) : null;
                n++;
                if (n >= data.RoleDetail[m].Authorize.length) {
                    n = 0;
                    m++;
                }
            }
            angular.forEach($scope.RoleDetail.RoleDetail, function (data, index, array) {
                angular.forEach(data.Authorize, function (data, index, array) {
                    if (RoleDetailIdList.length == 0) {
                        data.type = false;
                    }
                    for (var i = 0; i < RoleDetailIdList.length; i++) {
                        if (RoleDetailIdList[i] == data.Id) {
                            data.type = true;
                            RoleDetailIdList.splice(i, 1);
                            break;
                        } else {
                            data.type = false;
                        }
                    }
                });
            });

        }).error(function (data, status, headers, config) {
        });
    }
    function GetEmptyRoleDetail() {
        getsortConfig = {
            method: 'GET',
            url: '/role/GetEmptyRoleDetail',
        }
        $http(getsortConfig).success(function (data) {
            $scope.RoleDetail = data;
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.RolesModel.UserAuthorizeList = [];
});

//权限管理-菜单管理
meb.controller('authorityMenuController', function ($scope, $http, Msg, paging) {
    $scope.pagingModel = paging;
    var getsortConfig;
    //初始化模态窗
    $scope.md = Msg;
    //添加角色的属性
    $scope.MenuModel = {
        ParentPanelItem: 0
    }
    //编辑角色
    $scope.editorModel = {};
    init();
    //页面初始化
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/MBBackendPanel/GetJson',
            data: {
                menuPanel: $scope.MenuModel
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.MenuModel.PageIndex = index;
                init();
            });
        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    }
    //编辑菜单
    $scope.editorMsg = function (PanelItemId, PanelItemName, ParentPanelItem, Url) {
        $scope.editor = true;
        $scope.add = false;
        $scope.editorModel.PanelItemId = PanelItemId;
        $scope.editorModel.PanelItemName = PanelItemName;
        $scope.editorModel.ParentPanelItem = ParentPanelItem;
        $scope.editorModel.Url = Url;
        $scope.md.okShow('编辑菜单', '', function () {
            //编辑
            getsortConfig = {
                method: 'POST',
                url: '/MBBackendPanel/Update',
                data: {
                    mbBackendPanel: $scope.editorModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.editor = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.editor = false;
                $scope.md.show('提示', '数据获取失败');
            });
        });

    }
    $scope.remove = function (Id) {
        getsortConfig = {
            method: 'GET',
            url: '/MBBackendPanel/Delete?id=' + Id,
            data: {
                id: Id
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.editor = false;
            init();
            $scope.md.show('提示', data.Msg);
        }).error(function (data, status, headers, config) {
            $scope.editor = false;
            $scope.md.show('提示', '数据获取失败');
        });

    }
    $scope.addMsg = function () {
        $scope.editor = false;
        $scope.add = true;
        $scope.md.okShow('添加菜单', '', function () {
            //添加
            getsortConfig = {
                method: 'POST',
                url: '/MBBackendPanel/Add',
                data: {
                    mbBackendPanel: $scope.MenuModel
                }
            }
            $http(getsortConfig).success(function (data) {
                $scope.add = false;
                $scope.md.show('提示', data.Msg);
                init();
            }).error(function (data, status, headers, config) {
                $scope.md.show('提示', '数据获取失败');
            });
        });
    }
});

//权限管理-权限管理
meb.controller('userAuthorizeController', function ($scope, $http, $log, Msg, paging) {
    $scope.CompetenceModel = {};
    $scope.UserAuthorizeListConfig = {};
    $scope.pagingModel = paging;
    $scope.md=Msg;
    init();
    //页面初始化
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'POST',
            url: '/UserAuthorize/GetJson',
            data: {
                userAuthorizeSearchModel: $scope.UserAuthorizeListConfig
            }
        }
        $http(getsortConfig).success(function (data) {
            if ($scope.model!=null) {
                $scope.model.PageList = data.PageList;
            } else {
                if(data.Msg!=null){
                    $scope.Msg = false;
                    $scope.md.show('提示', data.Msg);
                } else {
                    $scope.model = data;
                    
                }
                
            }
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.UserAuthorizeListConfig.PageIndex = index;
                init();
            });
        }).error(function (data, status, headers, config) {
            $scope.Msg = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }
    $scope.search = function () {
        $scope.UserAuthorizeListConfig.PageIndex = null;
        init();
    }
    $scope.addCompetence = function () {
        $scope.Msg = true;
        $scope.CompetenceModel.Id = null;
        $scope.CompetenceModel.ControllName = null;
        $scope.CompetenceModel.ActionName = null;
        $scope.CompetenceModel.IsAllowedNoneRoles = null;
        $scope.CompetenceModel.Name = null;
        $scope.md.okShow('添加权限', '', function () {
            getsortConfig = {
                method: 'POST',
                url: '/UserAuthorize/Add',
                data: {
                    userAuthorize: $scope.CompetenceModel
                }
            }
            $log.log($scope.CompetenceModel);
            $http(getsortConfig).success(function (data) {
                if(data.Msg!=null){
                    $scope.Msg = false;
                    $scope.md.show('提示', data.Msg);
                }
                init();
                
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '添加失败');
            });
        });
    }
    $scope.editorMsg = function (Id,Name,MBBackendId,ControllName,ActionName,IsAllowedNoneRoles) {
        $scope.Msg = true;
        $scope.CompetenceModel.Id = Id;
        $scope.CompetenceModel.MBBackendId = MBBackendId;
        $scope.CompetenceModel.ControllName = ControllName;
        $scope.CompetenceModel.ActionName = ActionName;
        $scope.CompetenceModel.IsAllowedNoneRoles = ''+IsAllowedNoneRoles+'';
        $scope.CompetenceModel.Name = Name;
        $log.log($scope.CompetenceModel);
        $scope.md.okShow('编辑权限', '', function () {
            getsortConfig = {
                method: 'POST',
                url: '/UserAuthorize/Update',
                data: {
                    userAuthorize: $scope.CompetenceModel
                }
            }
            $http(getsortConfig).success(function (data) {
                init();
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '编辑失败');
            });
        });
    }
    $scope.remove = function (Id) {
        $scope.CompetenceModel.Id = Id;
        $scope.md.remove('删除', '确定删除该菜单？(删除后不可恢复)', function () {
            getsortConfig = {
                method: 'POST',
                url: '/UserAuthorize/Delete',
                data: {
                    userAuthorize: $scope.CompetenceModel
                }
            }
            $http(getsortConfig).success(function (data) {
                init();
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '编辑失败');
            });
        });
        
    }

});

//帮助管理-帮助文章管理
meb.controller('ArticleController', function ($scope, $http, $sce, Msg, paging) {

    var number = 0;
    var getsortConfig = {};
    $scope.pagingModel = paging;
    $scope.md = Msg;
    $scope.MenuModel = {};
    $scope.PageIndex = 1;
    $scope.articleModel = {}
    $scope.searchModel = {}
    $scope.Msg = false;
    init();
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Article/InitialPageLoaded?pageIndex=' + $scope.PageIndex,
            data: {

            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            //$scope.searchModel.Name = $scope.model.allRelation.Data[0].Name;
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.PageIndex = index;
                init();
            });
            console.log($scope.pagingModel);

        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    };
    //$scope.getNumber = function () {
    //    return number++;
    //}
    //添加
    $scope.addMsg = function () {

        $("#ContentFouse").blur();
        $("#titleStr").focus();
        $scope.Msg = true;
        $scope.articleModel.Title = '';
        $scope.articleModel.Id = 0;
        $scope.articleModel.Sort ='';
        $scope.articleModel.Contents = '';
        $scope.articleModel.Name = $scope.model.allRelation.Data[0].Name;
        $scope.articleModel.UserName = 'Admin';
        $scope.articleModel.CurrentLoginName = 'Admin'
        $("#NameError").attr("data-show", "false");
        $("#ContentsError").attr("data-show", "false");
        $("#IndexError").attr("data-show", "false");
        $scope.md.okShow('新增', '', function () {

            if ($scope.articleModel.Title == null || $scope.articleModel.Title.trim() == "") {
                $("#NameError").html("请输入标题").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Name.length > 50) {
                $("#NameError").html("最多输入50个字符").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Sort == null || $scope.articleModel.Sort.trim() == "") {
                $("#IndexError").html("请输入排序号").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }

            var indexStr = "^[0-9]{1,4}$";
            var regIndex = new RegExp(indexStr);
            if (!regIndex.test($scope.articleModel.Sort)) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if (parseInt($scope.articleModel.Sort) <= 0 || parseInt($scope.articleModel.Sort)>5000)
            {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            
            }
            if ($scope.articleModel.Contents == null || $scope.articleModel.Contents == "")
            {
                $("#ContentsError").html("请输入文章内容").attr("data-show", "true");
                $("#ContentFouse").focus();
                $scope.md.showMe = false;
                return false;
            }
            //if ($scope.articleModel.Contents.length > 500) {
            //    $("#ContentsError").html("内容不能为空且限制在500个字符以内").attr("data-show", "true");
            //    $("#ContentFouse").focus();
            //    $scope.md.showMe = false;
            //    return false;
            //}
            getsortConfig = {
                method: 'POST',
                url: '/Article/AddArticle',
                data: {
                    model: $scope.articleModel
                }
            };
            $http(getsortConfig).success(function (data) {
                $scope.Msg = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                init();
                $scope.md.show('提示', '数据获取失败');
            });

        });
    }
    // 编辑
    $scope.editorMsg = function (Id, Name, Title, Sort, Contents) {
    
        $("#ContentFouse").blur();
        $("#titleStr").focus();
        $("#NameError").attr("data-show", "false");
        $("#ContentsError").attr("data-show", "false");
        $("#IndexError").attr("data-show", "false");
        $scope.Msg = true;
        $scope.articleModel.Name = Name;
        $scope.articleModel.Title = Title;
        $scope.articleModel.Id = Id;
        $scope.articleModel.Sort = Sort;
        $scope.articleModel.Contents = Contents;

        $scope.md.okShow('编辑', '', function () {
            if ($scope.articleModel.Title == null || $scope.articleModel.Title.trim() == "") {
                $("#NameError").html("请输入标题").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Name.length > 50) {
                $("#NameError").html("最多输入50个字符").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Sort == null || $scope.articleModel.Sort == "") {
                $("#IndexError").html("请输入排序号").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }

            var indexStr = "^[0-9]{1,4}$";
            var regIndex = new RegExp(indexStr);
            if (!regIndex.test($scope.articleModel.Sort)) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if (parseInt($scope.articleModel.Sort) <= 0 || parseInt($scope.articleModel.Sort) > 5000) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;

            }
            if ($scope.articleModel.Contents == null || $scope.articleModel.Contents == "") {
                $("#ContentsError").html("请输入文章内容").attr("data-show", "true");
                $("#ContentFouse").focus();
                $scope.md.showMe = false;
                return false;
            }
            //if ($scope.articleModel.Contents.length > 500) {
            //    $("#ContentsError").html("内容不能为空且限制在500个字符以内").attr("data-show", "true");
            //    $("#ContentFouse").focus();
            //    $scope.md.showMe = false;
            //    return false;
            //}
            getsortConfig = {
                method: 'POST',
                url: '/Article/UpdateArticle',
                data: {
                    model: $scope.articleModel
                }
            };
            $http(getsortConfig).success(function (data) {
                $scope.Msg = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '编辑失败');
            });
        });
    }
    $scope.search = function () {
        getsortConfig = {
            method: 'POST',
            url: '/Article/GetSearchJsonData',
            data: {
                model: $scope.searchModel
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model.PageList = data.PageList;
            console.log('搜索完成');
            console.log(data);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.RecordCount, function (index) {
                $scope.PageIndex = index;
                init();
            });

        }).error(function (data, status, headers, config) {
            $scope.Msg = false;
            $scope.md.show('提示', '数据获取失败');
        });

    }
    $scope.remove = function (Id) {
        $scope.articleModel.Id = Id;
        getsortConfig = {
            method: 'POST',
            url: '/Article/DeleteArticle',
            data: {
                model: $scope.articleModel
            }
        };
        $http(getsortConfig).success(function (data) {
            $scope.Msg = false;
            init();
            $scope.md.show('提示', data.Msg);
        }).error(function (data, status, headers, config) {
            $scope.Msg = false;
            $scope.md.show('提示', '删除失败');
        });
    }
});

//帮助管理-帮助分类管理
meb.controller('ArticleRelationController', function ($rootScope, $scope, $http, Msg, paging) {
    var getsortConfig = {};
    $scope.pagingModel = paging;
    $scope.md = Msg;
    $scope.MenuModel = {};
    $scope.PageIndex = 1;
    $scope.articleModel = {}
    $scope.Msg = false;
    init();
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/ArticleRelation/InitialPage?pageIndex=' + $scope.PageIndex,
            data: {

            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            console.log($scope.model);
            //初始化分页
            $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                $scope.PageIndex = index;
                init();
            });
            console.log($scope.pagingModel);
        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    };

    //添加
    $scope.addMsg = function () {
        $("#IndexError").attr("data-show", "false");
        $("#NameError").attr("data-show", "false");
        $scope.Msg = true;
        $scope.articleModel.Name = null;
        $scope.articleModel.SequenceId = null;
        $scope.articleModel.Id = null;
        $scope.articleModel.UserName = 'Admin';
        $scope.articleModel.CurrentLoginName = 'Admin'
        $scope.md.okShow('添加', '', function () {
            //验证 
            if ($scope.articleModel.Name == null || $scope.articleModel.Name.trim() == "") {
                $("#NameError").html("请输入分类名称").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Name.length > 10) {
                $("#NameError").html("最多输入10个字符").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.SequenceId == null || $scope.articleModel.SequenceId.trim() == "") {
                $("#IndexError").html("请输入排序号").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }

            var indexStr = "^[0-9]{1,4}$";
            var regIndex = new RegExp(indexStr);
            if (!regIndex.test($scope.articleModel.SequenceId)) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if (parseInt($scope.articleModel.SequenceId) <= 0 || parseInt($scope.articleModel.SequenceId) > 5000) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;

            }
            getsortConfig = {
                method: 'POST',
                url: '/ArticleRelation/AddArticleReleation',
                data: {
                    module: $scope.articleModel
                }
            };
            $http(getsortConfig).success(function (data) {
                $scope.Msg = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '数据获取失败');
            });

        });
    }
    $scope.editorMsg = function (Id, Name, SequenceId) {
        $("#IndexError").attr("data-show", "false");
        $("#NameError").attr("data-show", "false");
        $scope.Msg = true;
        $scope.articleModel.Name = Name;
        $scope.articleModel.SequenceId = SequenceId;
        $scope.articleModel.Id = Id;
        $scope.md.okShow('修改', '', function () {
            //验证 
            if ($scope.articleModel.Name == null || $scope.articleModel.Name.trim() == "") {
                $("#NameError").html("请输入分类名称").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.Name.length > 10) {
                $("#NameError").html("最多输入10个字符").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if ($scope.articleModel.SequenceId == null || $scope.articleModel.SequenceId == "") {
                $("#IndexError").html("请输入排序号").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }

            var indexStr = "^[0-9]{1,4}$";
            var regIndex = new RegExp(indexStr);
            if (!regIndex.test($scope.articleModel.SequenceId)) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;
            }
            if (parseInt($scope.articleModel.SequenceId) <= 0 || parseInt($scope.articleModel.SequenceId) > 5000) {
                $("#IndexError").html("请输入(1-5000)正整数").attr("data-show", "true");
                $scope.md.showMe = false;
                return false;

            }
            getsortConfig = {
                method: 'POST',
                url: '/ArticleRelation/UpdateArticleRelation',
                data: {
                    model: $scope.articleModel
                }
            };
            $http(getsortConfig).success(function (data) {
                $scope.Msg = false;
                init();
                $scope.md.show('提示', data.Msg);
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '数据获取失败');
            });
        });
    }
    $scope.enable = function (Id, Name) {
        $scope.articleModel.Id = Id;
        $scope.articleModel.Name = Name;
        getsortConfig = {
            method: 'POST',
            url: '/ArticleRelation/EnableOrDisable',
            data: {
                module: $scope.articleModel
            }
        };
        $http(getsortConfig).success(function (data) {
            $scope.Msg = false;
            init();
            if (data.Msg != null) {
                $scope.md.show('提示', data.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.Msg = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }
});


//活动管理-优惠活动管理
meb.controller('ProductDiscountController', function ($rootScope, $scope, $http, $log, Msg, paging) {
    var getsortConfig = {};
    $scope.pagingModel = paging;
    $scope.md = Msg;
    $scope.activityModel = {
        productIds: []
    };
    $scope.searchModel = {};
    $scope.getArtivityConfig = {};
    $scope.Msg = false;
    init();
    function init() {
        var urls = '/ProductDiscount/GetJson?pageIndex=' + $scope.getArtivityConfig.PageIndex + '&state=' + $scope.getArtivityConfig.state + '&startTime=' + $scope.getArtivityConfig.startTime
        + '&endTime=' + $scope.getArtivityConfig.endTime + '&cstartTime=' + $scope.getArtivityConfig.cstartTime + '&cendTime=' + $scope.getArtivityConfig.cendTime;
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: urls,
        }
        $log.log($scope.getArtivityConfig);
        $http(getsortConfig).success(function (data) {
            $scope.model = data;
            $scope.model.ClasseList.length > 0 ? $scope.searchModel.classes = $scope.model.ClasseList[0].ClassId : null;
            $scope.model.GenreList.length > 0 ? $scope.searchModel.genre = $scope.model.GenreList[0].ProductGenreId : null;
            $scope.model.ProjectList.length > 0 ? $scope.searchModel.project = $scope.model.ProjectList[0].ParentId : null;
            $scope.model.AdvertList.length > 0 ? $scope.activityModel.advertId = $scope.model.AdvertList[0].Id : null;

            console.log($scope.model);
            if ($scope.model.PageList.Page != null) {
                //初始化分页
                $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, $scope.model.PageList.Page.PageSize, $scope.model.PageList.Page.RecordCount, function (index) {
                    $scope.getArtivityConfig.PageIndex = index;
                    init();
                });
            } else {
                $scope.pagingModel.paging = false;
            }

        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    };
    $scope.addMsg = function () {
        $scope.advType = true;
        $scope.Msg = true;
        $scope.activityModel.productIds = [];
        $scope.ProductList = [];
        $scope.activityModel.startTime = null;
        $scope.activityModel.endTime = null;
        //$scope.activityModel.discount=1;
        $scope.md.okShow('新增', '', function () {
            if ($scope.activityModel.advertId != null && $scope.activityModel.startTime != null && $scope.activityModel.endTime != null && $scope.activityModel.productIds != null) {
                $scope.activityModel.startTime = $scope.activityModel.startTime + ' 00:00:00';
                $scope.activityModel.endTime = $scope.activityModel.endTime + ' 23:59:59';
                var stringbind = '';
                for (var i = 0; i < $scope.activityModel.productIds.length; i++) {
                    if (i == 0) {
                        stringbind += $scope.activityModel.productIds[i].Id;
                    } else {
                        stringbind += ',' + $scope.activityModel.productIds[i].Id;
                    }
                }
                $scope.activityModel.productIds = stringbind;

                getsortConfig = {
                    method: 'POST',
                    url: '/ProductDiscount/Insert',
                    data: $scope.activityModel
                }
                $http(getsortConfig).success(function (data) {
                    $scope.Msg = false;
                    init();
                    if (data.Msg != null) {
                        $scope.md.show('提示', data.Msg);
                    }

                }).error(function (data, status, headers, config) {
                    $scope.Msg = false;
                    $scope.md.show('提示', '关联广告和活动开始、结束时间、商品不能为空');
                });
            } else {
                $scope.Msg = false;
                $scope.md.show('提示', '关联广告和活动开始、结束时间、商品不能为空');
            }
            
        });
    }
    $scope.searchProject = function () {
        getsortConfig = {
            method: 'POST',
            url: '/ProductDiscount/Search',
            data: $scope.searchModel
        }
        $http(getsortConfig).success(function (data) {
            $scope.ProductList = data.Data;
        }).error(function (data, status, headers, config) {

            $scope.Msg = false;
            
            $scope.md.show('提示', '数据获取失败');
        });
    }
    $scope.addProductId = function () {
        for (var i = 0, m = 0; i < $scope.ProductList.length;) {
            if ($scope.ProductList[i].Id == $scope.searchModel.selectProductList[m].Id) {
                $scope.ProductList.splice(i, 1);
                $scope.activityModel.productIds.push($scope.searchModel.selectProductList[m]);
                m = 0;
            } else {
                m++;
                if (m >= $scope.searchModel.selectProductList.length) {
                    i++;
                    m = 0;
                }

            }
        }
    }
    $scope.removeProductId = function () {
        $scope.ProductList;
        for (var i = 0, m = 0; i < $scope.activityModel.productIds.length;) {
            if ($scope.activityModel.productIds[i].Id == $scope.activityModel.selectProductList[m].Id) {
                $scope.activityModel.productIds.splice(i, 1);
                $scope.ProductList.push($scope.activityModel.selectProductList[m]);
                m = 0;
            } else {
                m++;
                if (m >= $scope.activityModel.selectProductList.length) {
                    i++;
                    m = 0;
                }

            }
        }
    }
    $scope.editorMsg = function (EditAdminId, startTime, endTime, AdvertId, Title) {
        $scope.activityModel.productIds = [];
        $scope.activityModel.EditAdminId = EditAdminId;
        $scope.activityModel.startTime = $scope.setDate(startTime,true);;
        $scope.activityModel.endTime = $scope.setDate(endTime,true);
        //$scope.activityModel.discount = discount;
        $scope.activityModel.advertId = AdvertId;
        $scope.activityModel.Title = Title;
        $scope.Msg = true;
        $scope.advType = false;
        console.log($scope.activityModel);

        getsortConfig = {
            method: 'POST',
            url: '/ProductDiscount/Edit',
            data: {
                advertId: AdvertId
            }
        }
        $http(getsortConfig).success(function (data) {
            $scope.activityModel.productIds = data.Data.Products;
        }).error(function (data, status, headers, config) {
        });

        $scope.md.okShow('编辑', '', function () {
            var stringbind = '';
            for (var i = 0; i < $scope.activityModel.productIds.length; i++) {
                if (i == 0) {
                    stringbind += $scope.activityModel.productIds[i].Id;
                } else {
                    stringbind += ',' + $scope.activityModel.productIds[i].Id;
                }
            }
            $scope.activityModel.productIds = stringbind;

            getsortConfig = {
                method: 'POST',
                url: '/ProductDiscount/Insert',
                data: $scope.activityModel
            }
            $http(getsortConfig).success(function (data) {
                $scope.Msg = false;
                init();
                if (data.Msg != null) {
                    $scope.md.show('提示', data.Msg);
                }
            }).error(function (data, status, headers, config) {
                $scope.Msg = false;
                $scope.md.show('提示', '数据获取失败');
            });
        });
    }
    $scope.$watch('activityModel.startTime', function (newVal, oldVal) {
        $scope.searchModel.startTime = newVal;
    });
    $scope.$watch('activityModel.endTime', function (newVal, oldVal) {
        $scope.searchModel.endTime = newVal;
    });


    $scope.remove = function (Id) {
        getsortConfig = {
            method: 'POST',
            url: '/ProductDiscount/Remove?advertId=' + Id,
        }
        $http(getsortConfig).success(function (data) {
            $scope.Msg = false;
            init();
            if (data.Msg != null) {
                $scope.md.show('提示', data.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.Msg = false;
            $scope.md.show('提示', '数据获取失败');
        });
    }

    $scope.search = function () {

        init();
    }
    $scope.setDate = function (date,t) {
        var d;
        if(t){
            d= new Date(date);
        }else{
            d = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
        }
        return d.getFullYear() + '-' + (d.getMonth() + 1 > 9 ? d.getMonth() + 1 : '0' + (d.getMonth() + 1)) + '-' + (d.getDate() > 9 ? d.getDate() : '0' + d.getDate());
    }
});

//报表统计-会员统计
meb.controller('UserReportController', function ($rootScope, $scope, $http, $log, paging) {
    $scope.pagingModel = paging;
    $scope.ReportConfig = {};
    init();
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Report/GetUserJson?pageIndex=' + $scope.ReportConfig.pageIndex + '&areaId=' + $scope.ReportConfig.areaId + '&sourceId=' + $scope.ReportConfig.sourceId + '&startTime=' + $scope.ReportConfig.startTime + '&endTime=' + $scope.ReportConfig.endTime + '&sourceId=' + $scope.ReportConfig.sourceId
        }
        console.log($scope.ReportConfig);
        $http(getsortConfig).success(function (data) {
            if ($scope.model != null) {
                $scope.model.PageList = data.PageList;
            } else {
                $scope.model = data;
                $scope.AreaList = [];
                angular.forEach(data.AreaList, function (data, index, array) {
                    angular.forEach(data.Children, function (data, index, array) {
                        $scope.AreaList.push(data);
                    });
                });
            }

            console.log($scope.model);
            //初始化分页
            if ($scope.model.PageList.Page != null) {
                $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, 10, $scope.model.PageList.Page.RecordCount, function (index) {
                    $scope.ReportConfig.pageIndex = index;
                    init();
                });
            } else {
                $scope.pagingModel.paging = false;
            }
            $log.log($scope.pagingModel);
        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    }
    $scope.search = function () {
        init();
    }
    $scope.$watch('ReportConfig.areas', function (newVal, oldVal) {
        newVal != null ? $scope.ReportConfig.areaId = newVal.Id : null;
    });
});

//报表统计-销售报表
meb.controller('MarketReportController', function ($rootScope, $scope, $http, $log, paging) {
    $scope.pagingModel = paging;
    $scope.ReportConfig = {};
    init();
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Report/GetMarketJson?pageIndex=' + $scope.ReportConfig.pageIndex + '&areaId=' + $scope.ReportConfig.areaId + '&sourceId=' + $scope.ReportConfig.sourceId + '&startTime=' + $scope.ReportConfig.startTime + '&endTime=' + $scope.ReportConfig.endTime + '&sourceId=' + $scope.ReportConfig.sourceId
        }
        $http(getsortConfig).success(function (data) {
            if ($scope.model != null) {
                $scope.model.PageList = data.PageList;
            } else {
                $scope.model = data;
                $scope.AreaList = [];
                angular.forEach(data.AreaList, function (data, index, array) {
                    angular.forEach(data.Children, function (data, index, array) {
                        $scope.AreaList.push(data);
                    });
                });
            }

            console.log($scope.model);
            //初始化分页
            if ($scope.model.PageList.Page != null) {
                $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, 10, $scope.model.PageList.Page.RecordCount, function (index) {
                    $scope.ReportConfig.pageIndex = index;
                    init();
                });
            } else {
                $scope.pagingModel.paging = false;
            }

        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    }
    $scope.search = function () {
        init();
    }
    $scope.$watch('ReportConfig.areas', function (newVal, oldVal) {
        newVal != null ? $scope.ReportConfig.areaId = newVal.Id : null;
    });
});


//报表统计-返退款报表
meb.controller('CashbackRefundController', function ($rootScope, $scope, $http, $log, paging) {
    $scope.pagingModel = paging;
    $scope.ReportConfig = {};
    init();
    function init() {
        //获取列表数据
        getsortConfig = {
            method: 'GET',
            url: '/Report/GetCashbackRefundJson?pageIndex=' + $scope.ReportConfig.pageIndex + '&areaId=' + $scope.ReportConfig.areaId + '&sourceId=' + $scope.ReportConfig.sourceId + '&startTime=' + $scope.ReportConfig.startTime + '&endTime=' + $scope.ReportConfig.endTime + '&sourceId=' + $scope.ReportConfig.sourceId
        }
        console.log($scope.ReportConfig);
        $http(getsortConfig).success(function (data) {
            if ($scope.model != null) {
                $scope.model.PageList = data.PageList;
            } else {
                $scope.model = data;
                $scope.AreaList = [];
                angular.forEach(data.AreaList, function (data, index, array) {
                    angular.forEach(data.Children, function (data, index, array) {
                        $scope.AreaList.push(data);
                    });
                });
            }

            console.log($scope.model);
            //初始化分页
            if ($scope.model.PageList.Page != null) {
                $scope.pagingModel.init($scope.model.PageList.Page.PageIndex, $scope.model.PageList.Page.PageCount, 10, $scope.model.PageList.Page.RecordCount, function (index) {
                    $scope.ReportConfig.pageIndex = index;
                    init();
                });
            } else {
                $scope.pagingModel.paging = false;
            }

        }).error(function (data, status, headers, config) {
            $scope.md.show('提示', '数据获取失败');
        });
    }
    $scope.search = function () {
        init();
    }
    $scope.$watch('ReportConfig.areas', function (newVal, oldVal) {
        newVal != null ? $scope.ReportConfig.areaId = newVal.Id : null;
    });
});


//导出Execl
function ExportExecl(action) {
    location.href = "/Report/ExportExecl?act=" + action;
}

